import { queryParkinglotNames } from '@/pages/Parking/Spot/service';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { queryPklotHistoryRevenue } from '../service';
import type { PklotRevenueTableItem } from '../types.d';

const RevenuePklotHistoryList = () => {
  const columns: ProColumns<PklotRevenueTableItem>[] = [
    {
      title: '路段名称',
      dataIndex: 'parkingLotId',
      hideInForm: true,
      align: 'center',
      render: (_, record) => record.parkingLotName,
      fieldProps: { showSearch: true },
      debounceTime: 300,
      request: (val) => queryParkinglotNames(val.keyWords),
    },
    {
      title: '统计日期',
      dataIndex: 'recordDay',
      hideInForm: true,
      valueType: 'date',
      align: 'center',
    },
    {
      title: '泊位数量',
      dataIndex: 'spotCount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '收费订单',
      dataIndex: 'chargedOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip: '统计时段内车辆驶出结算后，产生费用的订单数量（依据车辆驶出时间计算）',
    },
    {
      title: '录单率',
      dataIndex: 'chargePlatedOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip:
        '统计时段内车辆驶出结算后，产生费用的订单中，车牌已录入订单的占比（依据车辆驶出时间计算）',
      renderText: (_, record) => {
        if (record.chargedOrder == 0) return '0.00%';
        if (record.chargePlatedOrder == undefined) return '-';
        return ((record.chargePlatedOrder / record.chargedOrder) * 100).toFixed(2) + '%';
      },
    },
    {
      title: '免费订单',
      dataIndex: 'freeOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip: '统计时段内车辆驶出结算后，免费的订单数量（依据车辆驶出时间计算）',
    },
    {
      title: '异常订单',
      dataIndex: 'exceptOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip:
        '统计时段内车辆驶出结算后，未打印小票/未录入车牌/未缴清费用的订单（依据车辆驶出时间计算）',
    },
    {
      title: '追缴订单',
      dataIndex: 'recoveryOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip: '统计时段内追缴的订单数量（依据追缴时间计算）',
    },
    {
      title: '作废订单',
      dataIndex: 'cancelOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip: '统计时段内作废的订单数量（依据作废时间计算）',
    },
    {
      title: '会员订单',
      dataIndex: 'memberOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip: '统计时段内车辆驶出结算后，会员车辆的订单数量（依据车辆驶出时间计算）',
    },
    {
      title: '总订单',
      dataIndex: 'totalOrder',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      tooltip: '统计时段产生的订单数量（依据车辆驶入时间计算）',
    },
    {
      title: '泊位均收',
      dataIndex: 'payableAmount',
      key: 'avgPayableAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      valueType: 'money',
      renderText: (_, record) => {
        if (record.spotCount == 0) return 0;
        return record.payableAmount / 100 / record.spotCount;
      },
      tooltip: '总应收金额 / 泊位数量',
    },
    {
      title: '应收金额(总)',
      dataIndex: 'payableAmount',
      key: 'totalPayableAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      hideInTable: true,
      renderText: (_, record) => record.payableAmount / 100,
      valueType: 'money',
      tooltip: '统计时段内车辆驶出结算后，产生的总应收金额（依据车辆驶出时间计算）',
    },
    {
      title: '应收金额(已录车牌)', //plateAmount
      dataIndex: 'plateAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => (record.plateAmount || 0) / 100,
      valueType: 'money',
      tooltip:
        '统计时段内车辆驶出结算后，所有已录车牌的订单产生的应收金额（仅包含统计时间内驶出的订单）',
    },
    {
      title: '实收金额(已录车牌)',
      dataIndex: 'plateActualAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => (record.plateActualAmount || 0) / 100,
      valueType: 'money',
      tooltip:
        '统计时段内车辆驶出结算后，所有已录车牌的订单的实收金额（仅包含统计时间内驶出的订单）',
    },
    {
      title: '追缴金额',
      dataIndex: 'recoveryAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => record.recoveryAmount / 100,
      valueType: 'money',
      tooltip: '统计时段内追缴的总金额（依据追缴时间计算）',
    },
    {
      title: '欠费金额(已录车牌)',
      dataIndex: 'plateArrearAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => (record.plateArrearAmount || 0) / 100,
      valueType: 'money',
      tooltip:
        '统计时段内车辆驶出结算后，所有已录车牌的订单未缴清的总金额（仅包含统计时间内驶出的订单）',
    },
    {
      title: '会员免单金额',
      dataIndex: 'memberAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => record.memberAmount / 100,
      valueType: 'money',
      tooltip: '统计时段内车辆驶出结算后，会员车辆免单的费用（依据车辆驶出时间计算）',
    },
    {
      title: '作废金额',
      dataIndex: 'cancelAmount',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => record.cancelAmount / 100,
      valueType: 'money',
      tooltip: '统计时段内作废订单的应收金额(依据作废时间计算)',
    },
    {
      title: '缴费率',
      dataIndex: 'actualAmount',
      key: 'actualAmountAvg',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => {
        if (!record.plateAmount) return '-';
        return (((record.plateActualAmount || 0) / record.plateAmount) * 100).toFixed(2) + '%';
      },
      tooltip: '(实收金额(已录车牌) / 应收金额(已录车牌)) * 100',
    },
    {
      title: '欠费率',
      dataIndex: 'arrearAmount',
      key: 'arrearAmountAvg',
      align: 'center',
      hideInForm: true,
      hideInSearch: true,
      renderText: (_, record) => {
        if (!record.plateAmount) return '-';
        return ((record.plateArrearAmount / record.plateAmount) * 100).toFixed(2) + '%';
      },
      tooltip: '(欠费金额(已录车牌) / 应收金额(已录车牌)) * 100',
    },
  ];
  return (
    <PageHeaderWrapper
      header={{
        title: '',
        subTitle: '车场历史营收统计',
      }}
    >
      <ProTable<PklotRevenueTableItem>
        rowKey="_id"
        columns={columns}
        search={{ labelWidth: 'auto' }}
        scroll={{ x: true }}
        style={{ whiteSpace: 'nowrap' }}
        columnEmptyText="暂无数据"
        pagination={{
          showSizeChanger: true,
          showQuickJumper: true,
          defaultPageSize: 10,
          size: 'default',
        }}
        debounceTime={300}
        request={async (params, sort) => {
          return await queryPklotHistoryRevenue(params, sort);
        }}
      />
    </PageHeaderWrapper>
  );
};

export default RevenuePklotHistoryList;
